package BehaviorModel.strategyModel;

/**
 * 功能描述: 这个就是上下文对象
 * 2024/05/14
 * <p>
 * ZhangYi
 */
public abstract class Duck {
    private String name; // 鸭子名臣

    private FlyBehavior flyBehavior; // 飞翔行为;

    private SwimBehavior swimBehavior; // 游泳行为;

    private QuackBehavior quackBehavior; // 叫 的行为

    public void setFlyBehavior(FlyBehavior flyBehavior) {
        this.flyBehavior = flyBehavior;
    }

    public void setSwimBehavior(SwimBehavior swimBehavior) {
        this.swimBehavior = swimBehavior;
    }

    public void setQuackBehavior(QuackBehavior quackBehavior) {
        this.quackBehavior = quackBehavior;
    }

    public Duck(String name) {
        this.name = name;
    }



    public void fly(){
        if (flyBehavior == null){
            throw  new RuntimeException(this.name + " 飞的行为没有指定实现类 ");
        }
        System.out.println(name + flyBehavior.fly());
    }

    public void swim(){
        if (swimBehavior == null){
            throw  new RuntimeException(this.name+" 游泳的行为没有指定实现类 ");
        }
        System.out.println(name + swimBehavior.swim());
    }

    public void quack() {
        if (quackBehavior == null ){
            throw new RuntimeException(this.name + " 叫的行为没有指定实现类 ");
        }
        System.out.println(name + quackBehavior.quack());
    }

    // 默认行为
    public void display(){
        System.out.println(name + "祖先是鸭子");
    }
}
